<?php
/**
 * Created by JetBrains PhpStorm.
 * User: luca
 * Date: 11/06/13
 * Time: 15:05
 * To change this template use File | Settings | File Templates.
 */



add_filter('add_meta_box_modules','adleex_course_modules_box');
function adleex_course_modules_box() {
global $post;


    $query= adleex_get_query('modules_course',$post->ID);

    $columns = array(
        'title' => 'Modules',
        'post_status' => 'Status',
        'hour'	=> 'Hours'
    );
    $options = array(
        'singular' => 'Module',
        'plural' => 'Modules',
        'actions' => array('edit' => 'Edit', 'unlink' => 'Unlink'),
    );

    print_r($query);
    print_r(get_posts($query));

    admin_table_posts($options, $columns, $query );

    if ($post->post_status=='approved') {
        //$moduli_nonassociati_list = query_posts(adleex_get_query('modules_unassocied'));
        $moduli_nonassociati = '<option></option>';
        //foreach ($moduli_nonassociati_list as $id => $modulo) $moduli_nonassociati .= '<option value="'.$modulo->ID.'">'.$modulo->post_title.'</option>';
        echo '<select id="associate_module" name="associate_module">'.$moduli_nonassociati.'</select>';
        echo '<input type="submit" name="associate" id="associate-module" value="Associate"  class="button"/>';
        echo '<a href="post-new.php?post_type='.'adleex_module'.'&parent='.$post->ID.'" class="button">'._('Add new',ADLEEX_DOMAIN).'</a>';
    }

}





add_filter('course_field','adleex_course_render_field',10,2);
function adleex_course_render_field($field, $field_id) {
    global $post;

    switch ($post->post_status)
    {
        case 'draft':
            if (in_array($field_id, array('code', 'original_project', 'job_code', 'job_name', 'job_description', 'job_annuncement', 'job_annuncement_refer','job_deadline' ))){
                $field['readonly'] = false;
            }

            break;
        case 'pending':
        case 'publish':
            if (in_array($field_id, array('code', 'original_project'))){
                $field['readonly'] = true;
                $field['values'] = '';
            }
            break;
        case 'closed':
            $field['readonly'] = true;
            break;
    }

    return $field;
}

add_filter('add_meta_box_coursefinance','adleex_course_finance_box');
function adleex_course_finance_box(){
    global $post;

    $query = wpf_get_query('spese_corso', $post->ID);
    $columns = array(
        'title' => 'Spesa',
        'status' => 'Stato'
    );
    $options = array(
        'singular' => 'Spesa',
        'plural' => 'Spese',
        'ajax' => false,
        'actions' => array('edit' => 'Edit', 'unlink' => 'Unlink'),
        'comment' => 'code'
    );
    admin_table_posts($options, $columns, $query);

    if (in_array($post->post_status, array('pending', 'publish', 'ongoing')))
    {
        $nonassociate_list = query_posts(wpf_get_query('spese_nonassociate'));
        $nonassociate = '<option></option>';
        foreach ($nonassociate_list as $id => $spesa)
        {
            $nonassociate .= '<option value="' . $spesa->ID . '">' . $spesa->post_title . '</option>';
        }
        echo '<select id="associa_spesa" name="associa_spesa">' . $nonassociate . '</select>';
        echo '<input type="submit" name="associa" id="associa-spesa" value="Associa"  class="button"/>';
        echo '<a href="post-new.php?post_type=wpf_spesa&parent=' . $post->ID . '" class="button">' . _('Aggiungi nuovo') . '</a>';
    }
}


